//
//  RevanVisitorView.swift
//  weibo_swift
//
//  Created by 紫荆秋雪 on 16/12/15.
//  Copyright © 2016年 紫荆秋雪. All rights reserved.
//

import UIKit

class RevanVisitorView: UIView {
    /// 访客视图的信息字典 [imageName / message]
    /// 如果是首页 imageName == ""
    /// 访客视图字典信息的set方法
    var visitorInfo: [String: String]? {
        didSet {
            // 1> 取出字典信息
            guard let imageName = visitorInfo?["imageName"],
                let message = visitorInfo?["message"] else {
                return
            }
            
            // 2> 设置消息
            tipLabel.text = message
            
            // 3> 设置图像，首页不需要设置
            if imageName == "" {
                //首页动画
                startAnimation()
                return
            }
            // 隐藏小房子
            houseIconView.isHidden = true
            // 隐藏遮罩
            maskIconView.isHidden = true
            // 设置图片
            iconImageView.image = UIImage(named: imageName)
            
        }
    }
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //  私有方法
    private func startAnimation() {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.toValue = 2 * M_PI
        animation.repeatCount = MAXFLOAT
        animation.duration = 15
        
        //连续动画
        animation.isRemovedOnCompletion = false
        // 将动画添加到图层
        iconImageView.layer.add(animation, forKey: nil)
    }
    
    
    
    
    
    //  私有控件
    /// 转轮
    lazy var iconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    /// 遮罩
    lazy var maskIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    /// 小房子
    lazy var houseIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    /// 提示标签
    lazy var tipLabel: UILabel = UILabel(text: "关注一些人，回头看看有什么惊喜关注一些人，回头看看有什么惊喜", textColor: UIColor.darkGray)
    /// 注册按钮
    lazy var registerButton: UIButton = UIButton(textNormal: "注册", textSelect: "注册", textSize: 16, textNormalColor: UIColor.orange, textHightColor: UIColor.black, imageNormalName: "", imageHightName: "", backgroundImageName: "common_button_white_disable")
    
    /// 登陆按钮
    lazy var loginButton: UIButton = UIButton(textNormal: "登陆", textSelect: "登陆", textSize: 16, textNormalColor: UIColor.black, textHightColor: UIColor.black, imageNormalName: "", imageHightName: "", backgroundImageName: "common_button_white_disable")
}

// MARK: - 1、设置界面
extension RevanVisitorView {
    // 1-1、set UI
    func setupUI() {
        // 背景颜色
        backgroundColor = UIColor.revancolor(withRed: 237.0, green: 237.0, blue: 237.0, alpha: 1.0)
        
        /// 1、添加子控件
        addSubview(iconImageView)
        addSubview(maskIconView)
        addSubview(houseIconView)
        addSubview(tipLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        /// 2、取消autoresizing
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        /// 3、自动布局
        // 1> 图像视图
        addConstraint(NSLayoutConstraint(item: iconImageView,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: iconImageView,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerY,
                                         multiplier: 1.0,
                                         constant: -60))
        
        // 2>小房子
        addConstraint(NSLayoutConstraint(item: houseIconView,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: iconImageView, attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: houseIconView,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: iconImageView,
                                         attribute: .centerY,
                                         multiplier: 1.0,
                                         constant: 0))
        
        // 3>提示标题
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: iconImageView,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: iconImageView,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: 20))
        
        //设置文字宽度
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 236))
        
        // 4> 注册按钮
        addConstraint(NSLayoutConstraint(item: registerButton,
                                         attribute: .left,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .left,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: 20))
        addConstraint(NSLayoutConstraint(item: registerButton,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 100))
        
        // 5> 登陆按钮
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .right,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .right,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: 20))
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 100))
        
        // 6> 遮罩图片
        addConstraint(NSLayoutConstraint(item: maskIconView,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .top,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: maskIconView,
                                         attribute: .left,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .left,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: maskIconView,
                                         attribute: .right,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .right,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: maskIconView,
                                         attribute: .bottom,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: 0))
   
    }
}





